package com.gzhryc.shared_device.oem.micro.api;

import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

import com.gzhryc.common.gps.GPSEntity;
import com.gzhryc.common.gps.GPSLocationService;
import com.gzhryc.common.gps.IGPSDataService;
import com.gzhryc.common.gps.MemoryGPSDataService;
import com.gzhryc.common.logging.Logger;
import com.gzhryc.shared_device.oem.code.place.dao.db.PlacePhysicalStore;
import com.gzhryc.shared_device.oem.code.place.services.PlacePhysicalStoreService;
import com.gzhryc.shared_device.oem.code.place.services.dto.PlacePhysicalStoreSearch;

/**
 * 网点GPS服务
 * @version 2023-09-13
 */
public class PlaceGPSServiceManager {

    static Logger log = Logger.getLogger(PlaceGPSServiceManager.class);

    static PlaceGPSServiceManager me;

    public static PlaceGPSServiceManager self(){
        if(me == null){
            me = new PlaceGPSServiceManager();
        }
        return me;
    }

    IGPSDataService dataService;
    GPSLocationService locationService;
    WorkThread workThread;
    AtomicBoolean isRunning;

    public PlaceGPSServiceManager(){
        this.dataService = new MemoryGPSDataService();
        this.locationService = new GPSLocationService(this.dataService);
        this.isRunning = new AtomicBoolean(false);
    }

    public void start(){
        if(workThread != null){
            try {
                isRunning.set(false);
                workThread.interrupt();
                workThread = null;
            }catch (Exception e){
                log.error(e.getMessage(),e);
            }
        }
        isRunning.set(true);
        workThread = new WorkThread();
        workThread.start();
    }

    public GPSLocationService getLocationService(){
        return this.locationService;
    }

    public IGPSDataService getDataService(){return dataService;}

    /**
     * 工作线程
     */
     class WorkThread extends Thread {

        @Override
        public void run() {
            while (isRunning.get()) {
                try {
                    PlacePhysicalStoreSearch search = new PlacePhysicalStoreSearch();
                    Integer pageNo = 1;
                    Integer pageLimit = 300;
                    List<PlacePhysicalStore> physicalStoreList = PlacePhysicalStoreService.self().findJoinBySearch(search, pageNo, pageLimit);
                    while (physicalStoreList != null && physicalStoreList.size() > 0) {
                        for (PlacePhysicalStore physicalStore : physicalStoreList) {
                            if (physicalStore.getLatitude() != null && physicalStore.getLongitude() != null) {
                                GPSEntity gpsEntity = new GPSEntity();
                                gpsEntity.setId(physicalStore.getId());
                                gpsEntity.setLatitude(physicalStore.getLatitude());
                                gpsEntity.setLongitude(physicalStore.getLongitude());
                                dataService.save(gpsEntity);
                            }
                        }

                        pageNo = pageNo + 1;
                        physicalStoreList = PlacePhysicalStoreService.self().findJoinBySearch(search, pageNo, pageLimit);
                    }

                    log.info("初始化GPS位置数：" + dataService.getSize());

                    Thread.sleep(1 * 60 * 60 * 1000);       //1小时
                } catch (Exception e) {
                    log.error(e.getMessage(), e);
                }
            }
            log.error("GPS服务关闭");
            isRunning.set(false);
        }
    }
}
